-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
41 lines (34 loc) · 935 Bytes
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cout << "Enter the number of coin denominations: ";
cin >> n;
vector<int> coins(n);
cout << "Enter the coin denominations:" << endl;
for (int i = 0; i < n; i++) {
cin >> coins[i];
}
// Sort coins in descending order
sort(coins.begin(), coins.end(), greater<int>());
int amount;
cout << "Enter the amount: ";
cin >> amount;
int count = 0;
cout << "Coins used: ";
for (int coin : coins) {
while (amount >= coin) {
amount -= coin;
count++;
cout << coin << " ";
}
}
if (amount != 0) {
cout << "\nExact amount cannot be formed with the given denominations." << endl;
} else {
cout << "\nMinimum number of coins needed: " << count << endl;
}
return 0;
}